package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("1003")
public class VIPPaidRecordStrategy implements PaidRecordStrategy {
    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
//4.处理VIP购买记录 更新会员标识及过期时间
        //4.1 判断订单是否已经处理过
        LambdaQueryWrapper<UserVipService> userVipServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userVipServiceLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(userVipServiceLambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        //4.2 根据用户ID查询会员信息，判断是否是VIP
//        UserInfoVo userInfoVo = this.getUserInfo(userPaidRecordVo.getUserId());
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Boolean isVIP = false;
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            isVIP = true;
        }
        //4.3 根据套餐ID查询套餐信息，得到套餐服务月数
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //4.4 封装会员购买套餐记录且保存，计算套餐过期时间，
        UserVipService userVipService = new UserVipService();
        if(!isVIP){
            //新会员
            userVipService.setStartTime(new Date());

            userVipService.setExpireTime(DateUtil.offsetMonth(new Date(), serviceMonth));
        }else {
            //续费会员
            userVipService.setStartTime(DateUtil.offsetDay(userInfo.getVipExpireTime(), 1));
            userVipService.setExpireTime(DateUtil.offsetMonth(userVipService.getStartTime(), serviceMonth));
        }
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipServiceMapper.insert(userVipService);
        // 4.5 更新会员标识和过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
